#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void StInit(St* head)
{
	assert(head);
	StackDataType*tmp = (StackDataType*)malloc(sizeof(StackDataType) * 4);
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	head->data = tmp;
	head->size = 0;
	head->capcity = 4;
}

void StDestory(St* head)
{
	assert(head);
	head->size = 0;
	head->capcity = 0;
	free(head->data);
}

void StPush(St* head, StackDataType data)
{
	assert(head);
	if (head->size == head->capcity)
	{
		StackDataType* tmp = realloc(head->data, sizeof(StackDataType) * (head->capcity) * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		head->data = tmp;
		head->capcity *= 2;
	}
	head->data[head->size] = data;
	head->size++;
}

StackDataType  StPop(St* head)
{
	assert(head);
	if (head->size == 0)
	{
		return -1;
	}

	(head->size)--;

	return head->data[head->size];
}

void StPrint(St* head)
{
	assert(head);
	for (int i = head->size - 1; i >= 0; i--)
	{
		printf("%c ", head->data[i]);
	}
	printf("\n");
}